﻿using Unity.Interception.PolicyInjection.Pipeline;

namespace NoteHelper.Test
{
    /// <summary>
    /// 事务拦截器，必须继承ICallHandler
    /// </summary>
    public class TransactionInterceptor : ICallHandler
    {
        private log4net.ILog logger;
        public int Level { get; private set; }

        public int Order { get; set; }

        public TransactionInterceptor(int level, int order)
        {
            this.Level = level;
            this.Order = order;
            logger = log4net.LogManager.GetLogger(this.GetType());
        }

        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            logger.Info("开启事务");
            var result = getNext()(input, getNext);
            if (result.Exception == null)
            {
                logger.Info("提交事务");
            }
            else
            {
                logger.Info("事务回滚");
            }
            return result;
        }
    }
}
